package com.lzc;

import com.google.gson.Gson;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

public class Util {

    public static void writerData(HttpServletResponse resp, List list) {
        //9.将List对象转为JSON格式字符串
        Gson gson = new Gson();
        String result = gson.toJson(list);
        //10、获取输出流
//10.1、设置响应的内容类型
        resp.setContentType("application/json");
//10.2、设置响应的数据编码格式
        resp.setCharacterEncoding("utf-8");
//10.3、输出响应数据
        try {
            resp.getWriter().print(result);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 生成订单的编号
     */
    public static String createOrderId() {
        LocalDateTime now = LocalDateTime.now();
        return "" + now.getYear() + (now.getMonthValue() < 10 ? "0" + now.getMonthValue() : now.getMonthValue())
                + (now.getDayOfMonth() < 10 ? "0" + now.getDayOfMonth() : now.getDayOfMonth())
                + now.toLocalTime().toString().replace(".", "").replace(":", "");

    }
}


